package mc.support.taobao.api.fields;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA. <br/>
 * User: francis    <br/>
 * Date: 13-11-21    <br/>
 * Time: 10:38  <br/>
 */
public abstract class Fields {

    private static String convertToString(Set<Key> set) {
        StringBuilder s = new StringBuilder();

        int num = set.size();
        int i = 0;
        for ( Key k : set ) {
            if ( i < num - 1 ) {
                s.append( k.name() ).append( ',' );
            } else {
                s.append( k.name() );
            }
            i++;
        }

        return s.toString();
    }

    private Set<Key> set = new HashSet<>();

    protected Fields addField(Key k) {
        set.add( k );
        return this;
    }

    public Fields removeField(Key k) {
        set.remove( k );
        return this;
    }

    public boolean isSelectedAny() {
        return ! set.isEmpty();
    }

    @Override
    public String toString() {
        return toFieldsString();
    }

    public String toFieldsString() {
        if ( !set.isEmpty() ) {
            return convertToString( set );
        } else {
            return "";
        }
    }

}
